#!/usr/bin/env python
# -*- coding:utf-8 -*-

from django.shortcuts import redirect


class MiddlewareMixin(object):
    def __init__(self, get_response=None):
        self.get_response = get_response
        super(MiddlewareMixin, self).__init__()

    def __call__(self, request):
        response = None
        if hasattr(self, 'process_request'):
            response = self.process_request(request)
        if not response:
            response = self.get_response(request)
        if hasattr(self, 'process_response'):
            response = self.process_response(request, response)
        return response


class UserAuthMiddleware(MiddlewareMixin):

    def process_request(self, request):

        if request.path_info == "/login/":  # 这句话必须写,不然会发生无限递归
            return None

        if not request.session.get("user_info"):
            return redirect('/login/')
